# coding=utf-8
# 全世界最帅的帅哥
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
from pathlib import Path

PRIVATE_KEY_PATH = Path(__file__).parent.absolute() / "private.pem"


def decrypt(encrypted_data):
    private_key = RSA.import_key(open(PRIVATE_KEY_PATH).read())

    with open(encrypted_data, "rb") as f:
        enc_session_key = f.read(private_key.size_in_bytes())
        nonce = f.read(16)
        tag = f.read(16)
        ciphertext = f.read()

    # Decrypt the session key with the private RSA key
    cipher_rsa = PKCS1_OAEP.new(private_key)
    session_key = cipher_rsa.decrypt(enc_session_key)

    # Decrypt the data with the AES session key
    cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
    data = cipher_aes.decrypt_and_verify(ciphertext, tag)
    data = data.decode("utf-8")

    return data
